package com.example.yls.democ;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by yls on 2019/5/10.
 */

public class MyView extends View {
    private Paint paint = new Paint();

    //布局文件使用自定义MyView需要两个属性
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(2);    //设置画笔大小
        paint.setAntiAlias(true);  //设置反锯齿
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(30);
    }

    public MyView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cat);
        canvas.drawBitmap(bitmap, 0, 0, paint);

        Matrix matrix = new Matrix();
        //px,py:以px,py点进行旋转，1.5为像素比
        matrix.setRotate(90, 150, 150);
        canvas.drawBitmap(bitmap, matrix, paint);
        matrix.setRotate(180, 150, 150);
        canvas.drawBitmap(bitmap, matrix, paint);
        matrix.setRotate(270, 150, 150);
        canvas.drawBitmap(bitmap, matrix, paint);

        /*
        Matrix matrix = new Matrix();
        matrix.setScale(-1, 1);
        //宽*2*1.5
        matrix.postTranslate(300,0);
        canvas.drawBitmap(bitmap, matrix, paint);

        matrix.setScale(1, -1);
        matrix.postTranslate(0, 300);
        canvas.drawBitmap(bitmap, matrix, paint);

        matrix.setScale(-1, -1);
        matrix.postTranslate(300, 300);
        canvas.drawBitmap(bitmap, matrix, paint);
        */

        //canvas.drawPoint(100, 100, paint);
        //canvas.drawLine(100, 100, 400, 500, paint);
        //canvas.drawRect(100, 100, 400, 500, paint);
        //canvas.drawRoundRect(100, 100, 400, 500, 50, 50, paint);
        //canvas.drawCircle(200, 600, 50, paint);
        //画弧形，在左上角（100，100）和右下角（400，500）的矩形中心点从0度开始画90度
        //useCenter：true包含三角形部分（尖叫部分）
        //canvas.drawArc(100, 100, 400, 500, 0, 90, true, paint);
        //canvas.drawText("自信", 400, 100, paint);

        /*
        Point a = new Point(100, 0);
        Point b = new Point(30, 250);
        Point c = new Point(200, 100);
        Point d = new Point(0, 100);
        Point e = new Point(170, 250);
        Path path = new Path();
        path.moveTo(a.x, a.y);
        path.lineTo(b.x, b.y);
        path.lineTo(c.x, c.y);
        path.lineTo(d.x, d.y);
        path.lineTo(e.x, e.y);
        path.lineTo(a.x, a.y);
        canvas.drawPath(path, paint);
        canvas.drawText("闪烁的红星", 35, 280, paint);
        */
    }
}
